import crypto from 'crypto';
//⼆、实现散列算法
const hash = crypto.createHash('sha256');
hash.update('password1234');
console.log(hash.digest('hex'));
//三、⽣成随机字节
crypto.randomBytes(16, (err, buf) => {
    if (err) throw err;
    console.log(buf.toString('hex'));
   });
   //四、实现对称加密与解密
   //1. 使⽤AES-256-CBC加密：
    const algorithm = 'aes-256-cbc';
    const key = crypto.randomBytes(32);
    const iv = crypto.randomBytes(16);
    const cipher = crypto.createCipheriv(algorithm, key, iv);
    let encrypted = cipher.update('Hello, this is a secret message', 'utf8', 'hex');
    encrypted += cipher.final('hex');
    console.log(encrypted);
    //2. 解密操作：
    const decipher = crypto.createDecipheriv(algorithm, key, iv);
    let decrypted = decipher.update(encrypted, 'hex', 'utf8');
    decrypted += decipher.final('utf8');
    console.log(decrypted);